package com.study4j.generator;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.HashMap;
import java.util.Map;
import java.util.Scanner;

import com.baomidou.mybatisplus.core.exceptions.MybatisPlusException;
import com.baomidou.mybatisplus.generator.AutoGenerator;
import com.baomidou.mybatisplus.generator.InjectionConfig;
import com.baomidou.mybatisplus.generator.config.DataSourceConfig;
import com.baomidou.mybatisplus.generator.config.GlobalConfig;
import com.baomidou.mybatisplus.generator.config.PackageConfig;
import com.baomidou.mybatisplus.generator.config.StrategyConfig;
import com.baomidou.mybatisplus.generator.config.TemplateConfig;
import com.baomidou.mybatisplus.generator.config.rules.NamingStrategy;
import com.baomidou.mybatisplus.generator.engine.FreemarkerTemplateEngine;

import cn.hutool.core.util.StrUtil;

/**
 * @description 代码生成
 * @author Jiangbin
 * @date 2019年10月11日 上午11:52:11
 * @version 1.0.0
 */
public class CodeGenerator {
	/**
	 * 读取控制台内容
	 *
	 * @param tip
	 * @return
	 */
	public static String scanner(String tip) {
		Scanner scanner = new Scanner(System.in);
		StringBuilder help = new StringBuilder();
		help.append("请输入" + tip + "：");
		System.out.println(help.toString());
		if (scanner.hasNext()) {
			String ipt = scanner.next();
			if (StrUtil.isNotEmpty(ipt)) {
				return ipt;
			}
		}
		throw new MybatisPlusException("请输入正确的" + tip + "！");
	}

	public static void main(String[] args) {
		// 代码生成器
		AutoGenerator generator = new AutoGenerator();
		CodeGeneConfig codeGeneConfig = new CodeGeneConfig();
		// 全局配置
		GlobalConfig gc = new GlobalConfig();
		String projectPath = System.getProperty("user.dir");
		System.out.println("项目路径：" + projectPath);

		gc.setOutputDir(projectPath + "/codeTemp/");
		gc.setEntityName(codeGeneConfig.getProperty("gen.entity.name"));
		gc.setServiceName("%sService");
		gc.setMapperName("%sDao");
		gc.setAuthor(codeGeneConfig.getProperty("gen.author"));
		gc.setOpen(false);

		// gc.setSwagger2(true); 实体属性 Swagger2 注解
		generator.setGlobalConfig(gc);

		// 数据源配置
		DataSourceConfig dsc = new DataSourceConfig();
		dsc.setUrl(codeGeneConfig.getProperty("gen.url"));
		dsc.setSchemaName("public");
		dsc.setDriverName("com.mysql.cj.jdbc.Driver");
		dsc.setUsername(codeGeneConfig.getProperty("gen.username"));
		dsc.setPassword(codeGeneConfig.getProperty("gen.password"));
		generator.setDataSource(dsc);

		// 包配置
		PackageConfig pc = new PackageConfig();
		pc.setModuleName(scanner("模块名"));
		pc.setParent(codeGeneConfig.getProperty("gen.pkg.parent"));
		pc.setMapper("dao");
		generator.setPackageInfo(pc);

		// 配置模板
		TemplateConfig templateConfig = new TemplateConfig();
		templateConfig.setXml(null);
		generator.setTemplate(templateConfig);

		// 自定义配置
		InjectionConfig cfg = new InjectionConfig() {
			@Override
			public void initMap() {
				Map<String, Object> map = new HashMap<>(1);
				// 用来作为类注释的时间，模板中通过${cfg.datetime}获取
				map.put("datetime", new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(new Date()));
				this.setMap(map);
			}
		};
		generator.setCfg(cfg);

		// 策略配置
		StrategyConfig strategy = new StrategyConfig();
		strategy.setNaming(NamingStrategy.underline_to_camel);
		strategy.setColumnNaming(NamingStrategy.underline_to_camel);
		if (StrUtil.isNotBlank(codeGeneConfig.getProperty("gen.base.entity"))) {
			strategy.setSuperEntityClass(codeGeneConfig.getProperty("gen.base.entity"));
		}
		strategy.setEntityLombokModel(true);
		strategy.setRestControllerStyle(true);
		// 公共父类
		if (StrUtil.isNotBlank(codeGeneConfig.getProperty("gen.base.controller"))) {
			strategy.setSuperControllerClass(codeGeneConfig.getProperty("gen.base.controller"));
		}
		strategy.setInclude(codeGeneConfig.getProperty("gen.table.names").split(","));
		// 写于父类中的公共字段
		String superFields = codeGeneConfig.getProperty("gen.base.entity.fields");
		if (StrUtil.isNotBlank(superFields)) {
			strategy.setSuperEntityColumns(superFields.split(","));
		}
		strategy.setControllerMappingHyphenStyle(true);
		strategy.setTablePrefix(codeGeneConfig.getProperty("gen.table.prefix"));
		generator.setStrategy(strategy);
		generator.setTemplateEngine(new FreemarkerTemplateEngine());
		generator.execute();
	}
}
